package org.jrazdacha.bittorrent.modelling.core;

import java.util.BitSet;

public class Torrent {

	private static final int FIRST_BIT_INDEX = 0;
	private final int fileSize;
	private final int pieceSize;
	private final int pieceCount;
	private final BitSet pieceSet;

	public Torrent(int fileSize, int pieceSize) {
		this.fileSize = fileSize;
		this.pieceSize = pieceSize;
		this.pieceCount = fileSize / pieceSize;
		this.pieceSet = new BitSet(pieceCount);
	}
	
	/**
	 *  
	 * @return cloned bitfield of torrent
	 */
	public BitSet getBitfield() {
		return (BitSet)pieceSet.clone();		
	}

	public int getPieceCount() {
		return fileSize / pieceSize;
	}

	public boolean getPieceStatus(int pieceIndex) {
		return pieceSet.get(pieceIndex - 1);
	}

	public void complete() {
		pieceSet.set(FIRST_BIT_INDEX, pieceCount);
	}	
}
